// 表单验证
import moment from "moment";

// 简单验证11位手机号
export const checkPhone = (rule, value, callback) => {
    // new RegExp("^1[0-9]{10}$")
    const patter = new RegExp("^1[3-9][0-9]{9}$"); // 验证11位手机号
    if (!patter.test(value)) {
        return callback(new Error("请输入正确格式的手机号！"));
    } else {
        callback(); // 必须有此项回调，否则验证会一直不通过
    }
};

// 验证邮箱格式(可为空)
export const checkEmail = (rule, value, callback) => {
    // const mailReg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
    const mailReg = /^[a-zA-Z0-9]+([-_.][A-Za-zd0-9]+)*@([a-zA-Z0-9]+[-.])+[A-Za-zd]{2,5}$/
    if (value && !mailReg.test(value)) {
        return callback(new Error("请输入正确格式的邮箱！"));
    } else {
        callback();
    }
};

// 验证身份证号(可为空)
const checkIdNum = (rule, value, callback) => {
    const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
    if (value && !reg.test(value)) {
      return callback(new Error('证件号码不正确'))
    } else {
      callback()
    }
  }


// 验证身份证号
export const isCardID = (rule, sId, callback) => {
    var aCity = {
        11: "北京",
        12: "天津",
        13: "河北",
        14: "山西",
        15: "内蒙古",
        21: "辽宁",
        22: "吉林",
        23: "黑龙江",
        31: "上海",
        32: "江苏",
        33: "浙江",
        34: "安徽",
        35: "福建",
        36: "江西",
        37: "山东",
        41: "河南",
        42: "湖北",
        43: "湖南",
        44: "广东",
        45: "广西",
        46: "海南",
        50: "重庆",
        51: "四川",
        52: "贵州",
        53: "云南",
        54: "西藏",
        61: "陕西",
        62: "甘肃",
        63: "青海",
        64: "宁夏",
        65: "新疆",
        71: "台湾",
        81: "香港",
        82: "澳门",
        91: "国外"
    }
    let iSum = 0;
    let info = "";
    if (sId.length == 0) return callback();
    if (!/^\d{17}(\d|x)$/i.test(sId)) return callback(new Error("你输入的身份证长度或格式错误"));
    sId = sId.replace(/x$/i, "a");
    if (aCity[parseInt(sId.substr(0, 2))] == null) return callback(new Error("你的身份证地区非法"));
    let sBirthday = sId.substr(6, 4) + "-" + Number(sId.substr(10, 2)) + "-" + Number(sId.substr(12, 2));
    var d = new Date(sBirthday.replace(/-/g, "/"));
    if (sBirthday != (d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate())) return callback(new Error("身份证上的出生日期非法"));
    for (var i = 17; i >= 0; i--) iSum += (Math.pow(2, i) % 11) * parseInt(sId.charAt(17 - i), 11);
    if (iSum % 11 != 1) return callback(new Error("你输入的身份证号非法"));
    //aCity[parseInt(sId.substr(0,2))]+","+sBirthday+","+(sId.substr(16,1)%2?"男":"女");//此次还可以判断出输入的身份证号的人性别
    callback();
}



//获得字典中对应的中文名称
export const getLabel = (dictionary, value) => {
    const item = dictionary.filter(e => e.value == value)[0]

    return item && item.label
};

//  判断当前人是否 拥有 按钮的权限
export const judgePower = (Code) => {
    const authIdArr = JSON.parse(localStorage.getItem("localUserInfo")) && JSON.parse(localStorage.getItem("localUserInfo")).authIdArr

    if (authIdArr && authIdArr.includes(Code)) {
        return true
    } else {
        return false
    }
}

//导入临时窍 和 加入临时列表 ，返回信息提示
export const getTempReturnInfo = (allInfo, that) => {
    let messageArr = []

    if (allInfo.successBoxList && allInfo.successBoxList.length > 0) {
        messageArr.push(`${allInfo.successBoxList.length}窍上传成功`)
    }
    if (allInfo.existsTempList) {
        messageArr.push(`${allInfo.existsTempList.length>3 ? allInfo.existsTempList.splice(0,3) + '等' : allInfo.existsTempList}已加入临时列表，无需重复添加`)
    }
    if (allInfo.invalidBoxList) {
        messageArr.push(`窍 ${allInfo.invalidBoxList.length>3 ? allInfo.invalidBoxList.splice(0,3) + '等' : allInfo.invalidBoxList}已失效`)
    }
    if (allInfo.errorBoxList) {
        // messageArr.push('上传内容有误，请检查文件')
        messageArr.push(`窍 ${allInfo.errorBoxList.length>3 ? allInfo.errorBoxList.splice(0,3) + '等' : allInfo.errorBoxList}错误`)
    }

    if (!allInfo.invalidBoxList && !allInfo.existsTempList && !allInfo.errorBoxList) {
        that.$message({
            message: "操作成功",
            type: "success",
        });
    } else {
        that.$message({
            message: messageArr.toString(),
            type: "warning",
        });
    }
}

// 得到有权限的菜单 - 用于展示
export const getAuthRoute = (arrAgo, authIdArr, arr) => {
    arrAgo.forEach((e, index) => {
        if (!e.meta.authCode ||
            (e.meta.authCode &&
                authIdArr.includes(e.meta.authCode) &&
                arr.filter((m) => e.meta.authCode == m.meta.authCode).length == 0)
        ) {
            arr.push(
                e.children ? {
                    meta: e.meta,
                    path: e.path,
                    children: [],
                } : {
                    meta: e.meta,
                    path: e.path,
                }
            );
            if (e && e.children) {
                getAuthRoute(e.children, authIdArr, arr[index].children);
            }
            // this.$forceUpdate();
        }
    });
}

// 手机号脱敏 - 展示带星号的手机号 - 前端加密 - 去掉 - 用后端加密
export const showSecretPhone = (fullPhone) => {
    const secretPhone = fullPhone.replace(/(\d{3})\d*(\d{4})/, "$1****$2");
    // commonFun.showSecretPhone(alreadyPhone)

    return fullPhone
}

// 判断 当前是 手机端 还是pc端
export const os = (function() {
    var ua = navigator.userAgent,
        isWindowsPhone = /(?:Windows Phone)/.test(ua),
        isSymbian = /(?:SymbianOS)/.test(ua) || isWindowsPhone,
        isAndroid = /(?:Android)/.test(ua),
        isFireFox = /(?:Firefox)/.test(ua),
        isChrome = /(?:Chrome|CriOS)/.test(ua),
        isTablet =
        /(?:iPad|PlayBook)/.test(ua) ||
        (isAndroid && !/(?:Mobile)/.test(ua)) ||
        (isFireFox && /(?:Tablet)/.test(ua)),
        isPhone = /(?:iPhone)/.test(ua) && !isTablet,
        isPc = !isPhone && !isAndroid && !isSymbian;
    return {
        isTablet: isTablet,
        isPhone: isPhone,
        isAndroid: isAndroid,
        isPc: isPc,
    };
})();

// 获得当前的年份及前几年的年份
export const getYearArr = (num) => {
	const currentYear = new Date().getFullYear()
	let yearNum = currentYear - num
	return yearNum.toString()
}

export const getBoxInfoSelectOption = () => {
	const boxInfoSelectOption = [
		{
			label: "近三个月",
			value: "0",
			beginTime: moment().subtract(3, "months").valueOf(),
			endTime: moment().endOf('day').valueOf(),
		},
		{
			label: "今年",
			value: "1",
			beginTime: moment().startOf("year").valueOf(),
			endTime: moment().endOf("year").valueOf(),
		},
		{
			label: getYearArr(1) + "年",
			// "2024年",
			value: "9",
			beginTime: moment(getYearArr(1)).startOf("year").valueOf(),
			endTime: moment(getYearArr(1)).endOf("year").valueOf(),
		},
		{
			label: getYearArr(2) + "年",
			// "2023年",
			value: "8",
			beginTime: moment(getYearArr(2)).startOf("year").valueOf(),
			endTime: moment(getYearArr(2)).endOf("year").valueOf(),
		},
		{
			label: getYearArr(3) + "年",
			// "2022年",
			value: "7",
			beginTime: moment(getYearArr(3)).startOf("year").valueOf(),
			endTime: moment(getYearArr(3)).endOf("year").valueOf(),
		},
		{
			label: getYearArr(4) + "年",
			// "2021年",
			value: "2",
			beginTime: moment(getYearArr(4)).startOf("year").valueOf(),
			endTime: moment(getYearArr(4)).endOf("year").valueOf(),
		},
		{
			label: getYearArr(5) + "年",
			// "2020年",
			value: "3",
			beginTime: moment(getYearArr(5)).startOf("year").valueOf(),
			endTime: moment(getYearArr(5)).endOf("year").valueOf(),
		},
		{
			label: getYearArr(6) + "年",
			// "2019年",
			value: "4",
			beginTime: moment(getYearArr(6)).startOf("year").valueOf(),
			endTime: moment(getYearArr(6)).endOf("year").valueOf(),
		},
		{
			label: getYearArr(7) + "年",
			// "2018年",
			value: "5",
			beginTime: moment(getYearArr(7)).startOf("year").valueOf(),
			endTime: moment(getYearArr(7)).endOf("year").valueOf(),
		},
		{
			label: getYearArr(7) + "以前",
			// "2018年以前",
			value: "6",
			beginTime: moment("1970").startOf("year").valueOf(),
			endTime: moment(getYearArr(8)).endOf("year").valueOf(),
		},
	]

	return boxInfoSelectOption
}